package com.yxh.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.yxh.common.annotation.Authorization;
import com.yxh.pojo.Keng;
import com.yxh.pojo.Wordbook;
import com.yxh.pojo.Work;
import com.yxh.pojo.vo.WorkVO;
import com.yxh.service.WordbookService;
import com.yxh.service.WorkService;
import com.yxh.util.Response;
import com.yxh.util.SnowflakeIdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import static com.yxh.util.APIErrorCode.SUCCESS;

/**
 * Created by 32988 on 2018/6/12.
 */
@RestController
@RequestMapping("/work/")
public class WorkController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(WorkController.class);
    private Response response;

    @Autowired
    private WorkService workService;

    /**
     * 保存
     *
     * @param work
     * @return
     */
    @PostMapping("save_work")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response saveWork(@RequestBody Keng work) {
        work.setObjId(SnowflakeIdWorker.creatId());
        work.setType("2");
        workService.save(work);
        return Response.returnResponse(SUCCESS);
    }

    /**
     * 查询
     *
     * @param workVO
     * @return
     */
    @GetMapping("get_information")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response getInformation(Keng workVO, String page, String pageSize) {
        Integer limit;
        Integer offset;
        try {
            limit = Integer.parseInt(page);
            offset = Integer.parseInt(pageSize);
        } catch (Exception e) {
            limit = 1;
            offset = 10;
        }
        PageInfo pageInfo = workService.getInformation(workVO, limit, offset);
        response = Response.returnResponse(SUCCESS);
        response.setData(pageInfo);

        return response;
    }

    /**
     * 查询
     *
     * @param value
     * @return
     */
    @GetMapping("app")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response getInformationApp(String value, String page, String pageSize) {
        Integer limit;
        Integer offset;
        try {
            limit = Integer.parseInt(page);
            offset = Integer.parseInt(pageSize);
        } catch (Exception e) {
            limit = 1;
            offset = 10;
        }
        PageInfo pageInfo = workService.getInformation(value, limit, offset);
        response = Response.returnResponse(SUCCESS);
        response.setData(pageInfo);

        return response;
    }

    /**
     *修改
     * @param work
     * @return
     */
    @RequestMapping(value = "update_workInfo", method = RequestMethod.PUT)
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response updateWorkInfo(@RequestBody Keng work) {
        workService.update(work);
        response = Response.returnResponse(SUCCESS);
        return response;
    }

    /**
     * 删除
     * @return
     */
    @DeleteMapping("/{id}")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response deleteWork(@PathVariable("id") String id) {
        workService.delete(id);
        response = Response.returnResponse(SUCCESS);
        return response;
    }

    /**
     * 同步数据库
     *
     *
     * @return
     */
    @GetMapping("synchros")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response synchross() {

         response = Response.returnResponse(SUCCESS);
        //response.setData(pageInfo);
        workService.oacleToMysql();
        return response;
    }




}
